//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * buycarDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.buycar.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.buycar.dao.IBuycarDao;
import com.gamutsoft.amb.buycar.model.Buycar;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;

@SuppressWarnings("unchecked")
public class BuycarDaoHibernate extends GenericDaoHibernate<Buycar, Long> implements IBuycarDao {
    
     public BuycarDaoHibernate() {
		super(Buycar.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchBuycarList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.ADDDATE,T.COMMODITY_ID,T.USER_ID,T.STATUS FROM BUYCAR T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public Buycar searchBuycarById(Long id) throws BusinessException {
	
	  Buycar domain = new Buycar();
	   String hql = "from Buycar where id=?";
	   List<Buycar> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deleteBuycarById(Long id) throws BusinessException {
	
	 String hql ="delete from Buycar where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchGoodByUserId(Buycar domain) throws BusinessException {
		 StringBuffer sb = new StringBuffer();
			sb.append("SELECT T.ID,T.URL,T.DOMAINNAME,T.NAME,T.SHOPNAME,T.PRICE,T.BUYCOUNT,T.FREIGHT,T.REMARK,T.STATUSNODE,T.IMGURL,T.ORDERFORMINFOID,T.PACKAGESTATUS,T.PACKAGEID,T.CREATE_TIME ");
			sb.append(" FROM COMMODITY T left join BUYCAR B on T.id=B.commodity_id ");
			QueryExample qe =new QueryExample ();
			String mysql=" B.ID is not null ";
			if(domain!=null){
				if(domain.getUserId()!=null && !"".equals(domain.getUserId())){
					qe.addCondictions("B.USER_ID","=",domain.getUserId());
				}
				if(domain.getStatus()!=null && !"".equals(domain.getStatus())){
					qe.addCondictions("B.STATUS","=",domain.getStatus());
				}
				if(domain.getCommodityId()!=null &&!"".equals(domain.getCommodityId())){
					qe.addCondictions("B.COMMODITY_ID","=",domain.getCommodityId());
				}
			}
			qe.setMySql(mysql);
			qe.addOrderBy(" T.CREATE_TIME desc ");
			return this.findByQueryExample(sb.toString(), qe);
	}
	
}